<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="stuName">
        <el-input
          v-model="queryParams.stuName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帮扶老师" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入帮扶老师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="creationTime">
        <el-date-picker clearable
                        v-model="queryParams.creationTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预约时间" prop="appointmentTime">
        <el-date-picker clearable
                        v-model="queryParams.appointmentTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="问题类型" prop="issueType">
        <el-select v-model="queryParams.issueType" placeholder="请选择问题类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_issue_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="帮扶状态" prop="helpType">
        <el-select v-model="queryParams.helpType" placeholder="请选择帮扶状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_help_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:appointment:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:appointment:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appointmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="预约ID" align="center" prop="id" width="60"/>
      <el-table-column label="学生姓名" align="center" prop="stuName"/>
      <el-table-column label="帮扶老师" align="center" prop="teacherName"/>
      <el-table-column label="创建时间" align="center" prop="creationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creationTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" align="center" prop="appointmentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appointmentTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="issueType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_issue_type" :value="scope.row.issueType"/>
        </template>
      </el-table-column>
      <el-table-column label="帮扶状态" align="center" prop="helpType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_help_type" :value="scope.row.helpType"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="detail(scope.row)"
            v-hasPermi="['system:appointment:remove']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="setOk(scope.row)"
            v-hasPermi="['system:appointment:setOk']"
            :disabled="scope.row.helpType === 0 || scope.row.helpType === 3"
          >完成
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="over(scope.row)"
            v-hasPermi="['system:appointment:over']"
            :disabled="scope.row.helpType !== 2 "
          >移交
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:appointment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form2" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="stuName">
          <el-input v-model="form2.stuName" disabled placeholder="请输入学生姓名"/>
        </el-form-item>
        <el-form-item label="帮扶老师" prop="teacherName">
          <el-input v-model="form2.teacherName" disabled placeholder="请输入帮扶老师"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="creationTime">
          <el-date-picker
            disabled
            clearable
            v-model="form2.creationTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约时间" prop="appointmentTime">
          <el-date-picker
            disabled
            clearable
            v-model="form2.appointmentTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="问题类型" prop="issueType">
          <el-select v-model="form2.issueType" disabled placeholder="请选择问题类型">
            <el-option
              v-for="dict in dict.type.sys_issue_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帮扶状态" prop="helpType">
          <el-select v-model="form2.helpType" disabled placeholder="请选择帮扶状态">
            <el-option
              v-for="dict in dict.type.sys_help_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="完成时间">
          <el-date-picker
            disabled
            clearable
            v-model="form2.okTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="老师备注" prop="teacherNote">
          <el-input v-model="form2.teacherNote" disabled placeholder="请输入老师备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 移交 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form1" :rules="rules" label-width="80px">
        <el-form-item label="移交给谁" prop="takeName">
          <el-select v-model="form1.takeName" placeholder="请选择帮扶老师">
            <el-option
              v-for="dict in teacherAll"
              :label="dict.name"
              :value="dict.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="移交任务" prop="helpName">
          <el-input v-model="form1.helpName" placeholder="请输入移交任务"/>
        </el-form-item>
        <el-form-item label="移交备注" prop="transfeNote">
          <el-input v-model="form1.transfeNote" placeholder="请输入移交备注"/>
        </el-form-item>
        <div class="radio-container">
          <el-checkbox v-model="checked">给接手人发送邮件and 企业微信提醒</el-checkbox>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="goTransfer">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 完成对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="stuName">
          <el-input v-model="form.stuName" disabled placeholder="请输入学生姓名"/>
        </el-form-item>
        <el-form-item label="帮扶状态" prop="helpType">
          <el-select v-model="form.helpType" placeholder="请选择帮扶状态">
            <el-option
              v-for="dict in dict.type.sys_help_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老师备注" prop="teacherNote">
          <el-input v-model="form.teacherNote" placeholder="请输入老师备注"/>
        </el-form-item>
        <el-form-item label="完成时间" prop="okTime">
          <el-date-picker clearable
                          v-model="form.okTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
.radio-container {
  display: flex;
  justify-content: center;
}
</style>
<script>
import {
  getAdd,
  getTeacher,
  listAppointment,
  getAppointment,
  delAppointment,
  addAppointment,
  updateAppointment
} from "@/api/help/appointment";

export default {
  name: "Appointment",
  dicts: ['sys_help_type', 'sys_issue_type'],
  data() {
    return {
      checked: true,
      teacherAll: [],
      disabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 帮扶预约表格数据
      appointmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stuName: '',
        teacherName: '',
        creationTime: null,
        appointmentTime: null,
        issueType: null,
        helpType: null,
        okTime: null,
        teacherNote: ''
      },
      // 表单参数
      form: {},
      form1: {
        id: null,
        teacherName: '',
        helpName: '',
        transfeNote: '',
        checked: false,
        takeName: '',
      },
      form2: {},
      // 表单校验
      rules: {
        okTime: [
          {required: true, message: '完成时间不能为空!', trigger: 'blur'}
        ],
        takeName: [
          {required: true, message: '移交人不能为空!', trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    // 详情
    detail(row) {
      this.reset();
      getAppointment(row.id).then(response => {
        this.form2 = response.data;
        this.open2 = true;
        this.title = "预约详情";
      });
    },

    // 移交提交
    goTransfer() {
      let para = Object.assign({}, this.form1);
      para.checked = this.checked;
      getAdd(para).then(res => {
        this.$message({
          message: '移交成功!',
          type: 'success'
        });
        this.open1 = false;
        this.getList();
      })
    },

    // 获取老师下拉框
    geyTeacher() {
      getTeacher().then(res => {
        console.log(res.rows)
        this.teacherAll = res.data ;
      })
    },

    // 移交
    over(row) {
      this.reset();
      getAppointment(row.id).then(response => {
        this.form1 = response.data;
        this.open1 = true;
        this.title = "任务移交";
      });
    },

    /** 完成页面展示 */
    setOk(row) {
      this.reset();
      getAppointment(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "完成帮扶";
      });
    },

    /** 查询帮扶预约列表 */
    getList() {
      this.loading = true;
      listAppointment(this.queryParams).then(response => {
        this.appointmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuName: null,
        teacherName: null,
        creationTime: null,
        appointmentTime: null,
        issueType: null,
        helpType: null,
        okTime: null,
        teacherNote: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 完成帮扶提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.helpType === 2) {
            this.$message({
              message: "待处理状态不能设置完成时间",
              type: 'error'
            });
          } else {
            updateAppointment(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除帮扶预约编号为"' + ids + '"的数据项？').then(function () {
        return delAppointment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/appointment/export', {
        ...this.queryParams
      }, `appointment_${new Date().getTime()}.xlsx`)
    }
  },
  mounted() {
    this.geyTeacher();
  }
};
</script>
